package beautysalonsystem.controller;

import beautysalonsystem.Util.MailServiceUtil;
import beautysalonsystem.Util.StpCustomerUtil;
import beautysalonsystem.dto.OrderDTO;
import beautysalonsystem.mapper.OrderMapper;
import beautysalonsystem.mapper.UserMapper;
import beautysalonsystem.pojo.Customer;
import beautysalonsystem.pojo.Order;
import beautysalonsystem.result.Result;
import beautysalonsystem.service.OrderService;
import cn.dev33.satoken.annotation.SaCheckLogin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MailServiceUtil mailServiceUtil;

    @Autowired
    private OrderService orderService;

    //查询某一天的order信息
    @GetMapping()
    public Result getOrderByDate(@RequestParam
                                     @DateTimeFormat(pattern = "yyyyMMdd")
                                     LocalDate date){
        return Result.success(orderMapper.getOrderByDate(date));
    }

    //预定
    @PostMapping()
    public Result addOrder(@RequestBody Order order){
        String customerPhone = StpCustomerUtil.stp.getLoginIdAsString();
        Customer customer = userMapper.getCustomerByPhone(customerPhone);
        order.setCustomer(customer.getId());
        //如果该时间段已被预定
        int timeSlotAvailable = orderService.isTimeSlotAvailable(order);
        if(timeSlotAvailable == -1){
            return Result.error("当前时间已经超过预定时间,不可预定");
        } else if (timeSlotAvailable == -2) {
            return Result.error("该时间已经被预定了呢，换一个时间吧~");
        }
        orderMapper.addOrder(order);
        mailServiceUtil.sendBookingNotification(
                "2181037682@qq.com",  // 美容院的接收邮箱
                customer.getUsername(),
                order.getDate().toString()
        );
        return Result.success();
    }

    //删除预定（管理员端）
    @SaCheckLogin(type = "admin")
    @DeleteMapping("/{id}")
    public Result deleteOrder(@PathVariable Integer id){
        orderMapper.deleteOrder(id);
        return Result.success();
    }

    //审核约定（管理员端）
//    @SaCheckLogin(type = "admin")
//    @PutMapping("/review/{id}/{flag}")
//    public Result reviewOrder(@PathVariable Integer id, @PathVariable Integer flag){
//        //flag为1表示通过，flag为0表示退回
//        if(flag == 0){
//            orderMapper.deleteOrder(id);//将顾客的订单删除
//        }else {
//            orderMapper.updateOrderState(2);//确认订单
//        }
//        return Result.success();
//    }

    //修改预定的状态
    @SaCheckLogin(type = "admin")
    @PutMapping("/update/{id}/{status}")
    public Result updateOrder(@PathVariable Integer id, @PathVariable Integer status){
        Order orderById = orderMapper.getOrderById(id);
        orderById.setStatus(status);
        orderMapper.updateOrder(orderById);
        return Result.success();
    }
}
